﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters ;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;


namespace dhSystemBLL.produceBLL
{
   public  class packStockInBLL
    {
       private VpackbaobiaoTableAdapter _VpackbaobiaoAdapter = null;
       private VpackbaobiaoTableAdapter VpackbaobiaoAdapter
        {
            get
            {
                if (_VpackbaobiaoAdapter == null) _VpackbaobiaoAdapter = new VpackbaobiaoTableAdapter();
                return _VpackbaobiaoAdapter;

            }
        }
       private VpackCountTableAdapter  _packCountAdapter = null;
       private VpackCountTableAdapter packCountAdapter
        {
            get
            {
                if (_packCountAdapter == null) _packCountAdapter = new VpackCountTableAdapter();
                return _packCountAdapter;

            }
        }
       private VpackStockInSumTableAdapter _VpackStockInSumAdapter = null;
       private VpackStockInSumTableAdapter VpackStockInSumAdapter
        {
            get
            {
                if (_VpackStockInSumAdapter == null) _VpackStockInSumAdapter = new VpackStockInSumTableAdapter();
                return _VpackStockInSumAdapter;

            }
        }
        private V_packbiaoqianTableAdapter  _V_packbiaoqianAdapter = null;
        private V_packbiaoqianTableAdapter  V_packbiaoqianAdapter
        {
            get
            {
                if (_V_packbiaoqianAdapter == null) _V_packbiaoqianAdapter = new V_packbiaoqianTableAdapter();
                return _V_packbiaoqianAdapter;

            }
        }
       private packStockInMasterTableAdapter _packStockInMasterlAdapter = null;
       private packStockInMasterTableAdapter packStockInMasterAdapter
        {
            get
            {
                if (_packStockInMasterlAdapter == null) _packStockInMasterlAdapter = new packStockInMasterTableAdapter();
                return _packStockInMasterlAdapter;

            }
        }
       private packStockInDetailTableAdapter _packStockInDetailAdapter = null;
       private packStockInDetailTableAdapter packStockInDetailAdapter
        {
            get
            {
                if (_packStockInDetailAdapter == null) _packStockInDetailAdapter = new packStockInDetailTableAdapter();
                return _packStockInDetailAdapter;

            }
        }
        /// <summary>
       /// 查询包装入库主表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.packStockInMasterDataTable getpackStockInMasterAll()
        {
            try
            {
                return packStockInMasterAdapter.GetDatapackStockInMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 查询包装入库从表
        /// </summary>
        /// <returns></returns>

       public produceDataSet.packStockInDetailDataTable getpackStockInDetailAll()
        {
            try
            {
                return packStockInDetailAdapter.GetDatapackStockInDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 根据入库单号查询包装入库主表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.packStockInDetailDataTable getpackStockInDetailAllByorderDetailNo(string orderDetailNo)
        {
            try
            {
                return packStockInDetailAdapter.GetDatapackStockInDetailByorderDetailNo(orderDetailNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
       /// <summary>
       /// 根据漂染单号查询包装入库从表
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public produceDataSet.packStockInMasterDataTable getpackStockInMasterAllBybillNo(string billNo)
       {
           try
           {
               return packStockInMasterAdapter.GetDatapackStockInMasterALLBybillNo(billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据入库单号查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.V_packbiaoqianDataTable getV_packbiaoqianAll()
       {
           try
           {
               return V_packbiaoqianAdapter.GetDataV_packbiaoqian ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据入库单号查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.V_packbiaoqianDataTable getV_packbiaoqianAllBybillNo(string billNo)
       {
           try
           {
               return V_packbiaoqianAdapter.GetDataV_packbiaoqianBybillNo(billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据状态查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.VpackStockInSumDataTable getVpackStockInSumAllBybillStatus(string billStatus)
       {
           try
           {
               return VpackStockInSumAdapter.GetDataVpackStockInSumBybillStatus (billStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据状态查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.VpackStockInSumDataTable getVpackStockInSumAllBybillNo(string billNo)
       {
           try
           {
               return VpackStockInSumAdapter.GetDataVpackStockInSumBybillNo(billNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据状态查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.VpackCountDataTable getpackCountAll()
       {
           try
           {
               return packCountAdapter.GetDatapackCountALL ();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据状态查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.VpackCountDataTable getpackCountAllByorderDetailNo(string orderDetailNo)
       {
           try
           {
               return packCountAdapter.GetDataVpackCountByorderDetailNo (orderDetailNo);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据状态查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public baobiaoDataSet.VpackbaobiaoDataTable getVpackbaobiaoAllBybillStatus(string billStatus)
       {
           try
           {
               return VpackbaobiaoAdapter.GetDataVpackbaobiaoBybillStatus (billStatus);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
       /// <summary>
       /// 根据入库单号查询视图
       /// </summary>
       /// <param name="groupNum"></param>
       /// <returns></returns>
       public ViewDataSet.V_packbiaoqianDataTable getV_packbiaoqianAllByboxno(int  boxno)
       {
           try
           {
               return V_packbiaoqianAdapter.GetDataV_packbiaoqianByboxno (boxno);
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message.ToString());
           }
       }
        /// <summary>
       /// 根据入库单号查询包装入库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
       public produceDataSet.packStockInDetailDataTable getspackStockInDetailAllBybillNo(string billNo)
        {
            try
            {
                return packStockInDetailAdapter.GetDatapackstockInDetailBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
       /// 修改包装入库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int packStockInInsert(produceDataSet .packStockInMasterDataTable masterTable, produceDataSet.packStockInDetailDataTable pTable, string state, string billNo, string Date)
        {
            packStockInDetailModel packStockInDetailRow = new packStockInDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                packStockInMasterDAL packStockInMaster = new packStockInMasterDAL(tn, conn);
                packStockInDetailDAL packStockInDetail = new packStockInDetailDAL(tn, conn);
                packStockInMaster.packStockInMasterInsertII(masterTable, pTable);
               // basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    produceDataSet.packStockInMasterDataTable pdrkTable = getpackStockInMasterAllBybillNo(billNo);
                //    if (pdrkTable.Rows.Count == 0)
                //    {
                //        packStockInMaster.packStockInMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "P" + Date + "-" + basicSql.getOrderSerail("包装入库单号");
                //        packStockInMaster.packStockInMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("包装入库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "包装入库单号");
                //}
                //if (state == "编辑")
                //{
                //    packStockInMaster.packStockInMasterUpdate(row);
                //}


                //foreach (produceDataSet.packStockInDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        packStockInDetailRow.recordid = detailRow.recordid;
                //        packStockInDetailRow.billNo = detailRow.billNo;
                //        packStockInDetailRow.orderDetailNo = detailRow.orderDetailNo;
                //        packStockInDetailRow.pName = detailRow.pName;
                //        packStockInDetailRow.qty = detailRow.qty;
                //        packStockInDetailRow.unit = detailRow.unit;
                //        packStockInDetailRow.boxno = detailRow.boxno;
                //        packStockInDetailRow.qtywc = detailRow.qtywc;
                //        packStockInDetailRow.qtysy = detailRow.qtysy;
                //        packStockInDetailRow.createDate = DateTime.Now;
                //        if (!detailRow.IsmarkNull())
                //        {
                //            packStockInDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            packStockInDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        packStockInDetail.packStockInDetailInsert(packStockInDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        packStockInDetail.packStockInDetailUpdate(packStockInDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        packStockInDetail.packStockInDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行包装入库主表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int packStockInMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                packStockInMasterDAL packStockInMaster = new packStockInMasterDAL(tn, conn);
                packStockInMaster.packStockInDel (billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
       /// 删除一行包装入库主表
       /// </summary>
       /// <param name="row"></param>
       /// <returns></returns>
       public int packStockInDetailDel(string recordid)
       {
           int i = 0;
           SqlConnection conn = dataConn.connOpen();
           SqlTransaction tn = null;
           try
           {
               tn = conn.BeginTransaction();
               packStockInDetailDAL packStockInDetail = new packStockInDetailDAL(tn, conn);
               packStockInDetail.packStockInDetailDel (recordid);
               tn.Commit();
               return i;
           }
           catch (Exception ex)
           {
               tn.Rollback();
               throw new Exception(ex.Message.ToString());

           }
           finally
           {
               conn.Close();
           }
       }
    }
}
